#version 130

uniform sampler2DShadow ShadowMap;

uniform sampler2D Texture;

uniform vec3 cameraPosition;

varying vec4 ShadowCoord;
varying vec3 outNormal;
varying vec3 outColor;
uniform vec3 LightPosition;
varying vec3 position;
varying vec3 outPosition;
varying vec2 outUV;

const vec3 ambientColor=vec3(0.2,0.2,0.2);
const vec3 diffuseColor=vec3(0.7,0.7,0.7);
const vec3 specularColor=vec3(1.0,1.0,1.0);

uniform vec3 AmbientalLightIntensity;
uniform float bias;
uniform float shininess;
uniform int with_texture;

void main(){
	
	float shadowValue=0.0;
	shadowValue = textureProj(ShadowMap, ShadowCoord);
	
	vec3 color;
	if (with_texture == 1) {
		color= vec3(texture2D(Texture, outUV));
	} else {
		color = outColor;
	}
	
	vec3 normal = normalize(outNormal);
	vec3 surf2light = normalize(LightPosition - outPosition);
	vec3 surf2camera = normalize(cameraPosition - outPosition);
	vec3 reflection = -reflect(surf2camera,normal);
		
	float diffuseContribution = max(0.0,dot(normal,surf2light));
	float specularContribution = pow(max(0.0,dot(reflection,surf2light)),500.0);

	vec3 ambiental = AmbientalLightIntensity*color;
	//vec3 diffuse = diffuseContribution *diffuseColor*color;
	vec3 diffuse = (shadowValue)*diffuseContribution *diffuseColor*color;
	//vec3 specular = specularContribution*specularColor;
	vec3 specular = shadowValue<0.5 ? vec3(0.0,0.0,0.0) : specularContribution*specularColor*shadowValue;
	
	gl_FragColor=vec4( ambiental + diffuse + specular,1.0);
	//gl_FragColor=vec4( shadowValue,shadowValue,1.0,1.0);


}
